package common.util;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Liu_jc
 * Date: 13-8-30
 * Time: 下午5:48
 * To change this template use File | Settings | File Templates.
 */
public class ObjectUtils {

    public static boolean isNullOrEmptyString(Object o) {
        if(o == null)
            return true;
        if(o instanceof String) {
            String str = (String)o;
            if(str.length() == 0)
                return true;
        }
        return false;
    }

    /**
     * 可以用于判断 Map,Collection,String,Array是否为空
     * @param o
     * @return
     */
    @SuppressWarnings("all")
    public static boolean isEmpty(Object o)  {
        if(o == null) return true;

        if(o instanceof String) {
            if(((String)o).length() == 0){
                return true;
            }
        } else if(o instanceof Collection) {
            if(((Collection)o).isEmpty()){
                return true;
            }
        } else if(o.getClass().isArray()) {
            if(Array.getLength(o) == 0){
                return true;
            }
        } else if(o instanceof Map) {
            if(((Map)o).isEmpty()){
                return true;
            }
        }else {
            return false;
        }

        return false;
    }

    /**
     * 可以用于判断 Map,Collection,String,Array是否不为空
     * @param c
     * @return
     */
    public static boolean isNotEmpty(Object c) throws IllegalArgumentException{
        return !isEmpty(c);
    }

}
